$(function () {
    //数据表格
    var btn_datagrid = $("#btn_datagrid");
    btn_datagrid.datagrid({
        url:"/spending/list.do",//从远程站点请求数据
        fit:true,//面板大小将自适应父容器
        fitColumns:true,//自动展开/收缩列的大小
        striped:true,//显示斑马线效果
        // sortName:"",//默认排序列
        // sortOrder:"",//默认排序规则
        pagination:true,//显示分页工具栏
        columns:[[
            {field:"id",checkbox:true},//复选框
            {field:"time",title:"支出时间",width:80},
            {field:"paymoney",title:"支出金额",width:80},
            {field:"remark",title:"备注",width:80},
            {field:"payMan",title:"支出人",width:80,formatter:function (value) {
                    return value ? value.username : "";
                }},
            {field:"handleMan",title:"经手人",width:80,formatter:function (value) {
                    return value ? value.username : "";
                }},
            {field:"paytype",title:"支出方式",width:80},
            {field:"consumptiontype",title:"支出用途",width:80},
            {field:"sn",title:"单号",width:80},
            {field:"grade",title:"支出对应班级",width:80,formatter:function (value) {
                    return value ? value.className : "";
                }},
            {field:"auditMan",title:"审核人",width:80,formatter:function (value) {
                    return value ? value.username : "";
                }},
            {field:"state",title:"审核状态",width:80,formatter:function (value) {
                    return value ? "已审核" : "未审核";
                }}
        ]],
        toolbar:"#buttAll",
        //用户点击一行时触发
        onClickRow:function (index,row) {
            if (row.state) {
                $("#btn_edit").linkbutton('disable');
                $("#btn_audit").linkbutton('disable');
                $("#btn_rem").linkbutton('disable');
            }else {
                $("#btn_theAudit").linkbutton('disable');
            }
        },
        //用户取消选择一行的时候触发
        onUnselect:function (index,row) {
            $("#btn_edit").linkbutton('enable');
            $("#btn_audit").linkbutton('enable');
            $("#btn_rem").linkbutton('enable');
            $("#btn_theAudit").linkbutton('enable');
        }
    })

    //弹框
    btn_dialog = $("#btn_dialog");
    btn_dialog.dialog({
        width:330,
        height:380,
        buttons:"#btn_buttons",
        openAnimation:'show',
        closed:true,
        onClose:function () {
            myForm.form('clear');
        }
    })


    //弹框表单
    myForm = $("#myForm");
    myForm.form({

    })

    //新增编辑下拉框数据
    //支出人员
    $("#payMan").combobox({
        url:'/employee/list.do',//远程加载所有员工
        valueField:'id',//数据值名称绑定到该下拉列表框
        textField:'username',//数据字段名称绑定到该下拉列表框
    })
    //经手人员
    $("#handleMan").combobox({
        url:'/employee/list.do',//远程加载所有员工
        valueField:'id',//数据值名称绑定到该下拉列表框
        textField:'username',//数据字段名称绑定到该下拉列表框
    })
    // //审核人员
    // $("#audit").combobox({
    //     url:'/employee/list.do',//远程加载所有员工
    //     valueField:'id',//数据值名称绑定到该下拉列表框
    //     textField:'username',//数据字段名称绑定到该下拉列表框

    //支出类型
    $("#type").combobox({
        url:'/systemDictionaryItem/getItemBySn.do?sn=size',//远程加载所有支出类型
        valueField:'name',//数据值名称绑定到该下拉列表框
        textField:'name'//数据字段名称绑定到该下拉列表框
    })
    //支出班级
    $("#grade").combobox({
        url:'/grade/list.do',//远程加载所有班级
        valueField:'id',//数据值名称绑定到该下拉列表框
        textField:'className'//数据字段名称绑定到该下拉列表框
    })






    //高级查询框
    paytype = $("#paytype");
    payManId = $("#payManId");
    gradeId = $("#gradeId");
    paytype.combobox({//关键字查询
        url:'/systemDictionaryItem/getItemBySn.do?sn=size',//远程加载所有员工
        valueField:'name',//数据值名称绑定到该下拉列表框
        textField:'name'//数据字段名称绑定到该下拉列表框
    })
    payManId.combobox({
        url:'/employee/list.do',//远程加载所有员工
        valueField:'id',//数据值名称绑定到该下拉列表框
        textField:'username'//数据字段名称绑定到该下拉列表框
    })

    gradeId.combobox({
        url:'/grade/list.do',//远程加载所有班级
        valueField:'id',//数据值名称绑定到该下拉列表框
        textField:'className'//数据字段名称绑定到该下拉列表框
    })


    //高级查询方法
    methods = {
        add:function () {
            btn_dialog.dialog('open');
            btn_dialog.dialog('setTitle',"新增支出");
        },
        edit:function () {
            row = btn_datagrid.datagrid('getSelected');
            if (row == null) {
                $.messager.alert("温馨提示","至少需要选择一条数据","error");
                return;
            }
            console.log(row);
            // row["auditMan.id"] = row.auditMan.id;
            row["grade.id"] = row.grade.id;
            row["handleMan.id"] = row.handleMan.id;
            row["payMan.id"] = row.payMan.id;
            myForm.form('load',row);
            btn_dialog.dialog('open');
            btn_dialog.dialog('setTitle',"修改数据");
        },

        audit:function () {
            row = btn_datagrid.datagrid('getSelected');
            if (row == null) {
                $.messager.alert("温馨提示","至少需要选择一条数据","error");
                return;
            }
            $.messager.confirm("温馨提示","确认审核",function (r) {
                if (r) {
                    $.get('/spending/audit.do?id='+row.id,function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示","审核成功");
                            btn_datagrid.datagrid('reload');
                        }else {
                            $.messager.alert('温馨提示',data.msg,"error");
                        }
                    },"json");
                }
            })
        },

        rem:function () {
            row = btn_datagrid.datagrid('getSelected');
            if (row == null) {
                $.messager.alert("温馨提示","至少需要选择一条数据","error");
                return;
            }
            $.messager.confirm("温馨提示","确认删除",function (r) {
                if (r) {
                    $.get('/spending/rem.do?id='+row.id,function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示","删除成功");
                            btn_datagrid.datagrid('reload');
                        }else {
                            $.messager.alert('温馨提示',data.msg,"error");
                        }
                    },"json");
                }
            });
        },

        save:function () {
            myForm.form('submit',{
                url:'/spending/saveOrUpdate.do',
                success: function(){
                    $.messager.alert('温馨提示','保存成功');
                    methods["closes"]();
                    btn_datagrid.datagrid('reload');
                }
            })
        },
        closes:function () {
            myForm.form('clear');
            btn_dialog.dialog('close');
        },
        query:function () {
            gradeIdValue = gradeId.combobox('getValue');
            payManIdValue = payManId.combobox('getValue');
            paytypeValue = paytype.combobox('getValue');
            btn_datagrid.datagrid('load',{
                url:'/spending/list.do',
                gradeId:gradeIdValue,
                payManId:payManIdValue,
                paytype:paytypeValue
            })
        },
        theAudit:function () {
            row = btn_datagrid.datagrid('getSelected');
            if (row == null) {
                $.messager.alert("温馨提示","至少需要选择一条数据","error");
                return;
            }
            $.messager.confirm("温馨提示","确认审核",function (r) {
                if (r) {
                    $.get('/spending/theAudit.do?id='+row.id,function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示","审核成功");
                            btn_datagrid.datagrid('reload');
                        }else {
                            $.messager.alert('温馨提示',data.msg,"error");
                        }
                    },"json");
                }
            })
        }
    }

    //所有a标签点击事件触发的事件
    $("a[data-operation]").click(function () {
        met = $(this).data("operation");
        methods[met]();
    })


})